package handler

import (
	"context"
	"encoding/json"
	"errors"
	"time"
	"user-srv/basic/globar"
	__ "user-srv/basic/proto"
	"user-srv/model"

	"gorm.io/gorm"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Shopadd(_ context.Context, in *__.ShopaddReq) (*__.ShopaddResp, error) {
	var d model.Shop
	d = model.Shop{
		Name:        in.Name,
		Description: in.Description,
		Sort:        in.Sort,
	}
	if in.Name == "" {
		return nil, errors.New("名称不能为空")
	}
	if in.Sort <= 0 {
		return nil, errors.New("必须为正整数")
	}
	if err := d.GetOneFile(globar.DB); err != nil {
		return nil, errors.New("获取失败")
	}
	if err := d.Create(globar.DB); err != nil {
		return nil, errors.New("添加失败")
	}
	marshal, err := json.Marshal(d)
	if err != nil {
		return nil, err
	}
	globar.Rdb.Set(context.Background(), "数据", marshal, time.Minute*10)

	return &__.ShopaddResp{
		Id: int64(d.ID),
	}, nil
}

//// SayHello implements helloworld.GreeterServer
//func (s *Server) Shoptypetess(_ context.Context, in *__.ShoptypetessReq) (*__.ShoptypetessResp, error) {
//	var as model.Shop
//	globar.DB.Find(&as)
//	if as.ID <= 0 {
//		return nil, errors.New("id 错误")
//	}
//	var d model.Trees
//	d.Pid = int(as.ID)
//	d.Name = in.Name
//	globar.DB.Create(&d)
//	if in.Name == "" {
//		return nil, errors.New("名称不能为空")
//	}
//	return &__.ShoptypetessResp{
//		Id: int64(d.ID),
//	}, nil
//}

// SayHello implements helloworld.GreeterServer
func (s *Server) ShopUpdate(_ context.Context, in *__.ShopUpdateReq) (*__.ShopUpdateResp, error) {
	var as model.Shop
	globar.DB.Find(&as)
	if as.ID <= 0 {
		return nil, errors.New("id 错误")
	}
	var d model.Trees
	globar.DB.Find(&d)
	//if as.ID != uint(d.Pid) {
	//	return nil, errors.New("不是此分类下的无法修改")
	//}
	as = model.Shop{
		Model:       gorm.Model{ID: uint(d.Pid)},
		Name:        in.Name,
		Description: in.Description,
		Sort:        in.Sort,
	}
	marshal, err := json.Marshal(d)
	if err != nil {
		return nil, err
	}
	globar.Rdb.Set(context.Background(), "数据", marshal, time.Minute*10)
	return &__.ShopUpdateResp{
		Id: int64(d.ID),
	}, nil
}

// SayHello implements helloworld.GreeterServer
func (s *Server) ShopList(_ context.Context, in *__.ShopListReq) (*__.ShopListResp, error) {
	var as model.Shop
	cover, _ := as.GetCover(globar.DB)
	return &__.ShopListResp{
		List: cover,
	}, nil
}

// SayHello implements helloworld.GreeterServer
func (s *Server) ShopDel(_ context.Context, in *__.ShopDelReq) (*__.ShopDelResp, error) {
	var as model.Shop
	globar.DB.Find(&as)
	var t model.Trees
	globar.DB.Find(&t)
	if as.ID == uint(t.ID) {
		return nil, errors.New("主分类下面存在子分类不能进行删除 请先删除子分类")
	}
	if err := as.Del(globar.DB); err != nil {
		return nil, errors.New("删除失败")
	}
	marshal, err := json.Marshal(as)
	if err != nil {
		return nil, err
	}
	globar.Rdb.Set(context.Background(), "数据", marshal, time.Minute*10)
	return &__.ShopDelResp{
		Id: int64(as.ID),
	}, nil
}

// SayHello implements helloworld.GreeterServer
func (s *Server) ShopLike(_ context.Context, in *__.ShopLikeReq) (*__.ShopLikeResp, error) {
	var as model.Shop
	globar.DB.Where("name like ?", "%"+in.Name+"%").Find(&as)

	return &__.ShopLikeResp{
		Id:          int64(as.ID),
		Description: as.Description,
		Sort:        as.Sort,
		Name:        as.Name,
	}, nil
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Shoptypetess(_ context.Context, in *__.ShoptypetessReq) (*__.ShoptypetessResp, error) {
	var as model.Trees
	as = model.Trees{

		Pid:  0,
		Name: in.Name,
	}
	globar.DB.Create(&as)

	return &__.ShoptypetessResp{
		Id: int64(as.ID),
	}, nil
}
